<?php

namespace plugin\webman\xiangyaota;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;
use plugin\webman\xiangyaota\Method;

class Xiangyao
{
    public static function timerxy(){
        $time = 60-date('s',time());
        $data=[
            'code'=>1,
            'msg'=>'本局剩余时间',
            'info'=>(int)$time
        ];
        Gateway::sendToAll(Common::returndata('timer',$data));

        switch ($time) {
            case '60':
                $data=[
                    'code'=>1,
                    'msg'=>'游戏开始,请选择藏身房间',
                    'info'=>[]
                ];
                Gateway::sendToAll(Common::returndata('game_start',$data));
                break;
            case "55":  
                self::robottouzu();
                break;
            case "52":  
                self::robottouzu();
                break;            
            case "50":  
                self::robottouzu();
                break;
            case "47":  
                self::robottouzu();
                break;
            case "43":  
                self::robottouzu();
                break;
            case "36":  
                self::robottouzu();
                break;
            case "33":  
                self::robottouzu();
                break;
            case "29":  
                self::robottouzu();
                break;
            case "25":  
                self::robottouzu();
                break;
            case "22":  
                self::robottouzu();
                break;
            
            case '25':
                $data=[
                    'code'=>1,
                    'msg'=>'10秒后道士捉妖',
                    'info'=>[]
                ];
                Gateway::sendToAll(Common::returndata('game_killer_10',$data));
                break;
            case '15':
                $room = rand(0,7);
                // $roomname =  Common::smallroomname($room['killer_to_smallroomid']);
                $toroom['id'] =$room;
                $data=[
                    'code'=>1,
                    'msg'=>'道士开始捉妖',
                    'info'=>$toroom
                ];
        
                Gateway::sendToAll(Common::returndata('killerappear',$data));
                break;
            case '4':
                $room =  rand(0,7);
                $roomname =  Method::get_npc_name_byid($room);
                $data=[
                    'code'=>1,
                    'msg'=>'道长赶跑了【'.$roomname.'】，你成功躲过一劫，幸运者瓜分8588金币',
                    'info'=>$room
                ];
                
                // 开奖  核算每个房间真人投注量,随机开个房间
                // 杀手删掉了杂物间所有人幸存者瓜分66666金币
                
                Gateway::sendToAll(Common::returndata('result',$data));
                break;
        }
    }
    // 进入房间
    public static function enterroom($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
        if($checktoken==1){
            $expect = Common::getexpect();
            $user['coin']=Common::getusercoin($data['uid']);
            $user['allbet']=(int)Method::getuserallbet($data['uid'],$expect);
            // 各个妖怪的投注信息列表
            $list=Method::getallbetlist($expect);
            $info['user']=$user;
            $info['list']=$list;
            $data=[
                'code'=>1,
                'msg'=>'进入成功',
                'info'=>$info
            ];
            return $data;
        }
    }
    // 机器人投注
    public static function robottouzu(){
        // 挑选机器人出来
        // $rand = rand(1,5);
        // $max = Db::table('wa_users')->order("id desc")->limit(1)->first();
        // $max = Db::table('wa_users')->order("id desc")->limit(1)->first();
       

        // 获取表的总记录数
        $totalRecords = DB::table('wa_users')->count();
        
        // 生成一个随机数，该随机数介于0（包含）和表记录数（不包含）之间
        $randomNumber = rand(0, $totalRecords - 1);
        $rand = rand(1,3);
        
        // 使用limit方法，根据随机数限制查询结果的数量
        $randomRecords = DB::table('wa_users')
                        ->offset($randomNumber)
                        ->limit($rand)
                        ->get();
        
        // 输出随机选取的记录
        foreach ($randomRecords as $data) {
            // 模拟投注
            $expect = Common::getexpect();
            $npcid = rand(0,7);
            $coin = rand(1,100);
            $npcname=Method::get_npc_name_byid($npcid);
            $record = [
                'uid'=>$data->id,
                'npcid'=> $npcid,
                'npcname'=>$npcname,
                'coin'=>$coin,
                'expect'=>$expect,
                "is_robot"=>1,
                'ifwin'=>2,
                'reward'=>'0.00',
                'addtime'=>time(),
            ];
            $id = Db::table('wa_xyt_touzhu')->insert($record);
            // 增加redis的投注记录
            // $setredis = Method::setxytdata($record);
            $user['coin']=Common::getusercoin($data->id);
            // 用户总投注
            $user['allbet']=(int)Method::getuserallbet($record['uid'],$expect);
            // 各个妖怪的投注信息列表
            $list=Method::getallbetlist($expect);
            $info['user']=$user;
            $info['list']=$list;
            $data=[
                'code'=>1,
                'msg'=>'全局数据更新',
                'info'=>$info
            ];
            Gateway::sendToAll(Common::returndata('uplist',$data));
            return $data;

        }

    }

    public static function touzhu($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
        if($checktoken==1){

            $expect = Common::getexpect();
            // $expect =202311101048;
            $npcname = Method::get_npc_name_byid($data['npcid']);
            if(!$npcname || $npcname==1005){
                $data=[
                    'code'=>1001,
                    'msg'=>'妖怪不存在',
                    'info'=>[]
                ];
                return $data;
            }
            // 金币是否足够
            $usercoin = Common::getusercoin($data['uid']);
            if($usercoin<$data['coin']){
                $data=[
                    'code'=>1056,
                    'msg'=>'金币不足',
                    'info'=>[]
                ];
                return $data;
            }

            $record = [
                'uid'=>$data['uid'],
                'npcid'=>$data['npcid'],
                'npcname'=>$npcname,
                'coin'=>$data['coin'],
                'expect'=>$expect,
                "is_robot"=>0,
                'ifwin'=>2,
                'reward'=>'0.00',
                'addtime'=>time(),
            ];
            $id = Db::table('wa_xyt_touzhu')->insert($record);
            // 增加redis的投注记录
            $setredis = Method::setxytdata($record);

             // 扣除金币
            Common::updateusercoin($data['uid'],$data['coin'],0);
            
            if($id){
                // 给用户返回信息
                $user['coin']=Common::getusercoin($data['uid']);
                $user['allbet']=(int)Method::getuserallbet($data['uid'],$expect);
                // 各个妖怪的投注信息列表
                $list=Method::getallbetlist($expect);
                $info['user']=$user;
                $info['list']=$list;
                $data=[
                    'code'=>1,
                    'msg'=>'投注成功',
                    'info'=>$info
                ];
                return $data;
            }else{
                $data=[
                    'code'=>1010,
                    'msg'=>'投注失败',
                    'info'=>[]
                ];
                return $data;               
            }
        }
    }

    public static function tt(){
        Method::tt();
    }

}